package com.ymatou.iapi.anlimember.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.anlimember.enums.MemberTypeEnum;
import com.ymatou.iapi.anlimember.parameter.UpgradeReq;
import com.ymatou.iapi.anlimember.service.UpgradeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.anlihui.MemberRelation;
import com.ymttest.database.sqlwapper.anlihui.MemberRelationWrapper;

public class Ts_Upgrade {
	
	private static UpgradeReq upgradeReq;
	private static UpgradeCall upgradeCall;
	
	private static MemberRelationWrapper wrapper = new MemberRelationWrapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("手动升级");
	}

	@Before
	public void caseUp() {
		upgradeReq = new UpgradeReq();
		upgradeCall = new UpgradeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(upgradeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public static void verify() {

		MemberRelation actual = wrapper.selectByMemberId(upgradeReq.getMemberId());

		Logger.verifyEquals((byte) MemberTypeEnum.HAITAO_DAREN.getCode(),
				actual.getMember_level(), "验证Member_level");
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_001() {
		Logger.start(true, "安利会员邀新用户升级");
		try {
			
			Long memberId = 1111L;
			
			// 将会员等级更新成2，且可升级
			wrapper.updateMemberLevel(memberId, true, 2);
			
			upgradeReq.setMemberId(memberId);
			
			upgradeCall.setData(upgradeReq);
			upgradeCall.callService();
			
			verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_002() {
		Logger.start(true, "手动升级成功");
		try {
			
			Long memberId = 1111L;
			
			// 将会员等级更新成2，且可升级
			wrapper.updateMemberLevel(memberId, false, 2);
			
			upgradeReq.setMemberId(memberId);
			
			upgradeCall.setData(upgradeReq);
			upgradeCall.callService();
			
			Logger.verifyEquals(202, upgradeCall.getCode(), "验证code");
			Logger.verifyEquals(String.format("用户：%s不可升级", memberId),
					upgradeCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
